package asia.lxq.cic.controller;

import asia.lxq.cic.entity.Notice;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @GetMapping("/notice_list")
    public Result<Object> getLastNotice(@RequestParam(required = false,defaultValue = "college")String column,
                                        @RequestParam(required = false,defaultValue = "0")int value,
                                        @RequestParam(required = false,defaultValue = "1") int page,
                                        @RequestParam(required = false,defaultValue = "10")int pageSize){
        var columnName=switch (column){
            case "group"->"notice_group_id";
            case "college"->"notice_belong_id";
            default -> "notice_user_id";
        };
        return noticeService.getLastNoticeList(columnName, value, page, pageSize);
    }

    @PostMapping("/notice_post")
    public Result<Object> postNotice(@RequestBody Notice notice, Authentication authentication){
        int userId=Integer.parseInt(authentication.getPrincipal()+"");
        notice.setNoticeId(0);
        notice.setNoticeUserId(userId);
        return noticeService.postNotice(notice);
    }

}
